package com.jaychan.oauth;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author JayChan
 * @Description
 * @create 2022-01-11 9:43 PM
 */

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SecurityException.class)
    public ResponseEntity<ErrorBody> error(SecurityException e){
        log.warn("发生security异常",e);
        return new ResponseEntity<ErrorBody>(
                ErrorBody.builder()
                        .body("Token非法，用户不允许访问")
                        .status(HttpStatus.UNAUTHORIZED.value()).build(),
                HttpStatus.UNAUTHORIZED
        );
    }


}

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
class ErrorBody{
    private String body;
    private int status;
}
